package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategiesController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/detail")
    public Object detail(String id){

        strategyStatisVORedisService.increment(id, 1);

        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){

        Page<Strategy> query = strategyService.query(qo);

        return JsonResult.success(query);
    }

    @GetMapping("/themes")
    public Object themes(){

        return JsonResult.success(strategyThemeService.list());
    }

    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){

        BeanUtils.copyProperties(userInfo, comment);

        comment.setUserId(userInfo.getId());

        strategyCommentService.save(comment);

        strategyStatisVORedisService.increaseReplynum(comment.getStrategyId(), 1);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){

        Page<StrategyComment> page = strategyCommentService.query(qo);

        return JsonResult.success(page);
    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){

        strategyCommentService.commentThumb(cid, userInfo);

        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){

        StrategyStatisVO vo =  strategyStatisVORedisService.statisVo(sid);

        return JsonResult.success(vo);
    }

    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid, @UserParam UserInfo userInfo){

        boolean ble = userInfoRedisService.queryStrategyFavor(sid, userInfo);

        return JsonResult.success(ble);
    }

    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){

        boolean ble = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());

        return JsonResult.success(ble);
    }
}
